package net.huashitong.scheduling.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.scheduling.entity.*;
import net.huashitong.scheduling.filter.ProjectReserveFilter;
import net.huashitong.scheduling.service.TownProjectDetailService;
import net.huashitong.scheduling.service.TownProjectListService;
import net.huashitong.scheduling.service.TownRankDetailService;
import net.huashitong.scheduling.service.TownRankListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("app/townProject")
public class TownProjectController extends JsonBaseController {

    @Autowired
    private TownProjectListService townProjectListService;

    @Autowired
    private TownProjectDetailService townProjectDetailService;

    @Autowired
    private TownRankListService townRankListService;

    @Autowired
    private TownRankDetailService townRankDetailService;

    public static final String[] months = {"12", "11", "10", "09", "08", "07", "06", "05", "04", "03", "02", "01"};
    public static final String[] years={"2022", "2021","2020","2019","2018","2017","2016"};

    protected String getView(String arg0) {
        // TODO Auto-generated method stub
        return null;

    }

    @ResponseBody
    @RequestMapping("/queryTownList")
    public Message queryTownList(@RequestBody ProjectReserveFilter filter) {
        Map<String, Object> maps = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();
        int ss = 0;
        String date = "";
        String month = "";
        String year = "";
        if (ObjectUtils.isBlank(filter.getDate())) {
            for (int i = 0; i < ProjectRecommendationController.years.length; i++) {
                if (ss == 1) {
                    break;
                }
                for (int j = 0; j < ProjectRecommendationController.months.length; j++) {
                    List<TownProjectList> dateList = townProjectListService.queryByMonthAndYear(months[j], years[i]);
                    if (ObjectUtils.isNotBlank(dateList)) {
                        month = months[j];
                        year = years[i];
                        date = years[i] + "年" + months[j] + "月";
                        ss = 1;
                        break;
                    }
                }
            }
        } else {
            String months = filter.getDate().substring(4, 6);
            String years = filter.getDate().substring(0, 4);
            month = months;
            year = years;
            date = years + "年" + months + "月";
        }
        maps.put("date", date);
        filter.setMonth(month);
        filter.setYear(year);
        List<TownProjectList> townProjectLists = townProjectListService.queryByfilter(filter);
        List<String> lists = new ArrayList<>();
        for (TownProjectList townProjectList : townProjectLists) {
            Map<String, Object> map = new HashMap<>();
            map.put("projectList", townProjectList.getProjectList());
            map.put("count", townProjectList.getCount());
            map.put("parentId", townProjectList.getParentId());
            List<Map<String, Object>> townList=townProjectDetailService.querytownDetailListByMonthAndYear(townProjectList.getParentId(),filter);
             filter.setStudentId(townProjectList.getParentId());
            for(Map<String, Object> map1:townList){
                List<TownProjectDetail> townList2=townProjectDetailService.queryByTownDetailAndMonthAndYear(map1.get("townDetail").toString(),filter);
                map1.put("townCount",map1.get("townCount").toString());
                map1.put("list",townList2);
            }
            map.put("townList", townList);
            list.add(map);

        }
        String area = filter.getArea();
        if(StringUtils.isNotBlank(area)){
            List<Map<String, Object>> resultList = new ArrayList<>();
            for (Map<String, Object> objectMap : list) {
                List<Map<String, Object>> objectList = (List<Map<String, Object>>) objectMap.get("townList");
                for (Map<String, Object> map : objectList) {
                    if(map.get("townDetail").toString().contains(area)){
                        resultList.add(objectMap);
                        break;
                    }
                }

            }
            maps.put("list", resultList);
            return coverMessage("200", "成功获取数据", maps);
        }

        maps.put("list", list);
        return coverMessage("200", "成功获取数据", maps);
    }

    @ResponseBody
    @RequestMapping("/queryTownRankList")
    public Message queryTownRankList(@RequestBody ProjectReserveFilter filter) {
        Map<String, Object> maps = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();
        int ss = 0;
        String date = "";
        String month = "";
        String year = "";
        if (ObjectUtils.isBlank(filter.getDate())) {
            for (int i = 0; i < ProjectRecommendationController.years.length; i++) {
                if (ss == 1) {
                    break;
                }
                for (int j = 0; j < ProjectRecommendationController.months.length; j++) {
                    List<TownRankList> dateList = townRankListService.queryByMonthAndYear(months[j], years[i]);
                    if (ObjectUtils.isNotBlank(dateList)) {
                        month = months[j];
                        year = years[i];
                        date = years[i] + "年" + months[j] + "月";
                        ss = 1;
                        break;
                    }
                }
            }
        } else {
            String months = filter.getDate().substring(4, 6);
            String years = filter.getDate().substring(0, 4);
            month = months;
            year = years;
            date = years + "年" + months + "月";
        }
        maps.put("date", date);
        filter.setMonth(month);
        filter.setYear(year);
        List<TownRankList> townRankLists=townRankListService.queryByFilter(filter);
        int s=1;
        for(TownRankList townRankList:townRankLists){
            //"0"表示头部的信息
            if("0".equals(townRankList.getType())) {
                if ("总".equals(townRankList.getProjectList())) {
                    maps.put("totalInvestment", townRankList.getTotalInvestment());
                    maps.put("totalCount", townRankList.getCount());
                    maps.put("unit","亿元");
                }
                if (townRankList.getProjectList().contains("累计")) {
                    maps.put("completeNumberTwo", townRankList.getCompleteNumber());
                    maps.put("percentageTwo", townRankList.getPercentage());
                }

            }
            //"1"表示乡镇排名列表
            if ("1".equals(townRankList.getType())){
                Map<String, Object> map = new HashMap<>();
                map.put("category",townRankList.getCategory());
                map.put("townList",townRankList.getProjectList());
                map.put("percentage",townRankList.getPercentage());
                map.put("planNumber",townRankList.getPlanNumber());
                map.put("completeNumber",townRankList.getCompleteNumber());
                map.put("parentId",townRankList.getParentId());
                map.put("rank",s);
                list.add(map);
            }

            s++;
        }
        maps.put("list",list);

        return coverMessage("200", "成功获取数据", maps);
    }


    @ResponseBody
    @RequestMapping("/queryTownRankThird")
    public Message queryTownRankThird(@RequestBody ProjectReserveFilter filter) {
        Map<String, Object> maps = new HashMap<>();
        int ss = 0;
        String date = "";
        String month = "";
        String year = "";
        if (ObjectUtils.isBlank(filter.getDate())) {
            for (int i = 0; i < ProjectRecommendationController.years.length; i++) {
                if (ss == 1) {
                    break;
                }
                for (int j = 0; j < ProjectRecommendationController.months.length; j++) {
                    List<TownRankList> dateList = townRankListService.queryByMonthAndYear(months[j], years[i]);
                    if (ObjectUtils.isNotBlank(dateList)) {
                        month = months[j];
                        year = years[i];
                        date = years[i] + "年" + months[j] + "月";
                        ss = 1;
                        break;
                    }
                }
            }
        } else {
            String months = filter.getDate().substring(4, 6);
            String years = filter.getDate().substring(0, 4);
            month = months;
            year = years;
            date = years + "年" + months + "月";
        }
        maps.put("date", date);
        filter.setMonth(month);
        filter.setYear(year);

        TownRankList townRankList=townRankListService.queryByFilterAndParentId(filter);
        maps.put("planNumber",townRankList.getPlanNumber());
        maps.put("completeNumber",townRankList.getCompleteNumber());
        maps.put("rank",filter.getRank());
        maps.put("projectList",townRankList.getProjectList());
        maps.put("increaseByMonth",townRankList.getIncreaseMonth());
       List<Map<String, Object>> list = new ArrayList<>();
      String months = filter.getDate().substring(4,6);
       String years = filter.getDate().substring(0,4);
       filter.setYear(years);
       filter.setMonth(months);
       List<TownRankDetail> townRankDetails=townRankDetailService.queryByFilterAndStudentId(filter);
       for(TownRankDetail townRankDetail:townRankDetails){
           Map<String, Object> map = new HashMap<>();
           map.put("productName",townRankDetail.getProductName());
           map.put("planCount",townRankDetail.getPlanCount());
           map.put("completeCount",townRankDetail.getCompleteCount());
           list.add(map);
       }
       maps.put("list",list);

        return coverMessage("200", "成功获取数据", maps);
    }

    @ResponseBody
    @RequestMapping("/queryTownProjectDetail")
    public Message queryTownProjectDetail(@RequestBody ProjectReserveFilter filter){
        Map<String, Object> map = new HashMap<>();
        TownProjectDetail tpd=townProjectDetailService.getById(filter.getSysId());
        map.put("sysId",tpd.getSysId());
        map.put("projectName",tpd.getProjectName());
        map.put("month",tpd.getMonth());
        map.put("year",tpd.getYear());
        map.put("totalInvestment",tpd.getTotalInvestment());
        map.put("currentYearInvestment",tpd.getCurrentYearInvestment());
        map.put("bigType",tpd.getBigType());
        map.put("smallType",tpd.getSmallType());
        map.put("constructionContent",tpd.getConstructionContent());
        map.put("ownerUnit",tpd.getOwnerUnit());
        return coverMessage("200", "成功获取数据", map);
    }

}
